package com.hx.vending.controller;

import com.hx.vending.annotation.Accredit;
import com.hx.vending.annotation.Authorization;
import com.hx.vending.annotation.CurrentAdminId;
import com.hx.vending.bean.AdminRole;
import com.hx.vending.bean.Menu;
import com.hx.vending.bean.entity.MenuVO;
import com.hx.vending.service.IAdminRoleService;
import com.hx.vending.service.IMenuService;
import com.hx.vending.util.Msg;
import com.hx.vending.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/12/29.
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;
    @Autowired
    private IAdminRoleService adminRoleService;

    /**
     * 获取菜单列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    @Accredit
    public Object list(@RequestParam(required = false) String menuName,@CurrentAdminId Integer adminId,
                       @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        AdminRole a = new AdminRole();
        a.setAdminRoleAdminId(adminId);
        Integer[] roleList = adminRoleService.selectRoleByAdmin(adminId);
        if (roleList.length <=0){
            roleList = null;
            return Msg.success().add("va_mag",new ArrayList<>());
        }
        Integer[] menus = menuService.selectMenuByRole(roleList);
        if (menus.length <= 0){
            menus = null;
            return Msg.success().add("va_mag",new ArrayList<>());
        }
        List<MenuVO> menuVOS = this.menuService.selectVoByExample(menuName,menus);
        for(MenuVO menuVO:menuVOS){
            Menu m1 = new Menu();
            m1.setMenuSuperiorId(menuVO.getMenuId());
            List<Menu> menuList = menuService.selectByExample(m1);
            menuVO.setMenuList(menuList);
        }
        return Msg.success().add("va_msg",menuVOS);
    }

    /**
     * 获取菜单列表
     */
    @RequestMapping(value = "/menuList")
    @ResponseBody
    public Object menuList() {
        Integer[] menus = menuService.selectMenu();
        if (menus.length <= 0){
            menus = null;
            return Msg.success().add("va_mag",new ArrayList<>());
        }
        List<MenuVO> menuVOS = this.menuService.selectVoByExample(null,menus);
        for(MenuVO menuVO:menuVOS){
            Menu m1 = new Menu();
            m1.setMenuSuperiorId(menuVO.getMenuId());
            List<Menu> menuList = menuService.selectByExample(m1);
            menuVO.setMenuList(menuList);
        }
        return Msg.success().add("va_msg",menuVOS);
    }


    /*
    * 添加菜单
    * */
    @RequestMapping("/saveMenu")
    @ResponseBody
    @Accredit
    public Msg saveMenu(@RequestBody Menu menu,BindingResult result){
        if(result.hasErrors()){
            return Msg.fail().add("msg","请求有错误");
        }
        //设置父级菜单编号
        Msg msg = menuSetPcode(menu);
        if(msg.getCode() == 200){
            menu.setMenuState(1);
            int i = menuService.insertMenu(menu);
            if(i > 0){
                return Msg.success();
            }
        }
        return Msg.fail();
    }
    /*
    * 修改菜单
    * */
    @RequestMapping(value = "/edit")
    @ResponseBody
    @Accredit
    public Msg edit(@RequestBody Menu menu, BindingResult result) {
        if (result.hasErrors()) {
            return Msg.fail().add("msg","请求有错误");
        }
        //设置父级菜单编号
        int i = menuService.updateByPrimaryKey(menu);
        if(i > 0){
                return Msg.success();
            }
        return Msg.fail();
    }

    /**
     * 删除菜单
     * @param menuIds 菜单id（多个用“-”隔开）
     * @return
     */
    @RequestMapping(value = "/remove/{menuIds}",method = RequestMethod.DELETE)
    @ResponseBody
    @Accredit
    public Msg remove(@PathVariable("menuIds") String menuIds) {
        if (ToolUtil.isEmpty(menuIds)) {
            return Msg.fail().add("msg","请求有错误");
        }
        Msg msg = menuService.delMenuContainSubMenus(menuIds);
        if(msg.getCode() == 200){
            return Msg.success();
        }else{
            return Msg.fail();
        }
    }
    /*
    * 设置父级菜单编号
    * */
    private Msg menuSetPcode(Menu menu){
        if (ToolUtil.isEmpty(menu.getMenuSuperiorId()) || menu.getMenuSuperiorId().equals(0)) {
            menu.setMenuSuperiorId(0);
            menu.setMenuLevels(1);
        }else{
            Integer id = menu.getMenuSuperiorId();
            Menu m = new Menu();
            m.setMenuId(id);
            Menu pMenu = menuService.selectByPrimaryKey(m);
            Integer pLevels = pMenu.getMenuLevels();
            menu.setMenuSuperiorId(pMenu.getMenuId());
            //如果编号和父编号一致会导致无限递归
            /*if (menu.getMenuId().equals(menu.getMenuSuperiorId())) {
                return Msg.fail().add("msg","菜单和父菜单不能一致");
            }*/
            menu.setMenuLevels(pLevels+1);
        }
        return Msg.success();
    }
}
